Skip to content

Share code that two places wrote twice, in the Core internals and the two pumps - #304

Merged
electricessence merged 1 commit into
masterfrom
refactor/shared-internals
Dec 2, 2026
Merged

electricessence merged 1 commit into
masterfrom
refactor/shared-internals

Conversation

@electricessence

Copy link
Copy Markdown
Collaborator

Internal refactor from a look at the code added since rc.16: places that wrote the same thing twice now share it. The net is 11 source lines removed (206 added, 217 deleted, 18 files): small, because the helpers add lines and the largest candidate was left out (below).

No public API changes. Every change is internal, or a message or a name that no caller reads.

  • The Channel and Dataflow pumps share two helpers in Core: the one that
    disposes an enumerator that will not be read, and the ones that link a
    caller's token to a stream's stall expiry and recognize an expiry that
    the caller did not cancel. Each pump keeps its own try/catch and its
    per-row loops.
  • DirectStream and StallStream take the stall timeout exception and the
    "enumerated only once" exception from one place, with the same texts.
  • A chunk of a stream is released with a using: the lease is IDisposable
    (Dispose releases it, as Release does).
  • ChunkChain.Dispose asks Used for the rows of a chunk, as the copy out
    does. ChunkSizes checks its power-of-two bounds with one helper.
    PooledBuffer.From builds its config in one place.
  • The "matches none of the parameters" exception has one text for a
    command and for a query (the words before the declared names stay as
    they were).
  • ExpressiveCommandBase asks for the declared text names in one place, and
    builds the refusal of ExecuteAsync with captures in one place.
  • ExpressiveDbCommand passes a connection function straight to its base,
    as its siblings do. A null function now names the parameter
    connFactory in the ArgumentNullException (it named the adapter's
    parameter, factory).
  • The procedure schema code: the table that GetSchema returns is
    disposed, the "rows from more than one schema" scan and the collection
    name are shared by the names and the metadata, the two one-line
    wrappers around the row reader are gone, one helper reads an integer
    that may not be one, and the exception filter that mixes is and &&
    is parenthesized.
  • MappedDbQuery builds its changed copies in one method; the two view
    executors share the canceled-token answer; the command view makes one
    query core instead of two.
  • The 776 of the SQL Server procedure lookup is a named constant with the
    arithmetic.

Cost: nothing per row or per call. The new shared calls sit on the setup
of a pass (the link), on a throw path (the exceptions) or on a dispose.
The stream iterator that releases a chunk with a using may carry one more
struct in its state machine (once per stream, not per chunk).

Two behavior notes (neither is a code path anyone depends on)

  • A null connection function given to an ExpressiveDbCommand now reports ArgumentNullException.ParamName as connFactory (it was the adapter's factory).
  • In the procedure names read, the "rows from more than one schema" scan now runs before the rows are read: the result is the same; only a malformed ordinal after a schema mismatch no longer throws first.

Left out

Reading the procedure's parameter names and its metadata with one GetSchema call: the names come through the protected virtual GetProcedureParameterNamesAsync (an extension point; the SQL Server package reads names with its own query), so sharing the table needs hidden per-command state or a new virtual. That is a design change, not a refactor.

Checks

No benchmark was run: the shared calls are on setup, throw and dispose paths only, as the cost line says.
Release 15,990 and Debug 15,969 tests pass (16 skipped), Generator tests 189, the package consumer tests with the generator on and off (net11.0 and net472), 0 build warnings on every target framework, 255 sample checks, VS MSBuild 0 errors, dotnet format style and analyzers clean on all 28 checks, DocFX 0 warnings, the style scan clean. No test was changed.

🤖 Generated with Claude Code

… two pumps

No public API changes. Every change is internal, or a message or a name that no caller reads.

- The Channel and Dataflow pumps share two helpers in Core: the one that
  disposes an enumerator that will not be read, and the ones that link a
  caller's token to a stream's stall expiry and recognize an expiry that
  the caller did not cancel. Each pump keeps its own try/catch and its
  per-row loops.
- DirectStream and StallStream take the stall timeout exception and the
  "enumerated only once" exception from one place, with the same texts.
- A chunk of a stream is released with a using: the lease is IDisposable
  (Dispose releases it, as Release does).
- ChunkChain.Dispose asks Used for the rows of a chunk, as the copy out
  does. ChunkSizes checks its power-of-two bounds with one helper.
  PooledBuffer.From builds its config in one place.
- The "matches none of the parameters" exception has one text for a
  command and for a query (the words before the declared names stay as
  they were).
- ExpressiveCommandBase asks for the declared text names in one place, and
  builds the refusal of ExecuteAsync with captures in one place.
- ExpressiveDbCommand passes a connection function straight to its base,
  as its siblings do. A null function now names the parameter
  connFactory in the ArgumentNullException (it named the adapter's
  parameter, factory).
- The procedure schema code: the table that GetSchema returns is
  disposed, the "rows from more than one schema" scan and the collection
  name are shared by the names and the metadata, the two one-line
  wrappers around the row reader are gone, one helper reads an integer
  that may not be one, and the exception filter that mixes `is` and `&&`
  is parenthesized.
- MappedDbQuery builds its changed copies in one method; the two view
  executors share the canceled-token answer; the command view makes one
  query core instead of two.
- The 776 of the SQL Server procedure lookup is a named constant with the
  arithmetic.

Cost: nothing per row or per call. The new shared calls sit on the setup
of a pass (the link), on a throw path (the exceptions) or on a dispose.
The stream iterator that releases a chunk with a using may carry one more
struct in its state machine (once per stream, not per chunk).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@electricessence
electricessence merged commit e53a5bc into master Dec 2, 2026
9 checks passed
@electricessence
electricessence deleted the refactor/shared-internals branch December 2, 2026 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant